package com.buba.service.impl;

import com.buba.mapper.FileMapper;
import com.buba.mapper.FollowTopicMapper;
import com.buba.mapper.KudosMapper;
import com.buba.mapper.TopicMapper;
import com.buba.service.TopicService;
import entity.*;
import entity.VO.FollowTopicVO;
import entity.VO.ReleaseTopicVO;
import entity.VO.TopicVO;
import entity.VO.UserVO;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * LC: 2023-02-20 20:03
 */
@Service
public class TopicServiceImpl implements TopicService {

    @Autowired
    TopicMapper topicMapper;
    @Autowired
    FileMapper fileMapper;
    @Autowired
    KudosMapper kudosMapper;
    @Autowired
    FollowTopicMapper followTopicMapper;

    /**
     * 查询话题列表
     * @return
     */
    @Override
    public List<Map> getTopicLabel(String sort,int id) {
        //获取话题列表
        List<ReleaseTopicVO> topicLabel = topicMapper.getTopicLabel(sort);
        //创建List
        List<Map> list = new ArrayList<>(topicLabel.size());
        //获取该id关注的话题
        List<FollowTopicVO> followTopicList = followTopicMapper.getFollowTopicList(id);
        for (ReleaseTopicVO releaseTopicVO : topicLabel) {
            //获取话题的被关注数量
            int followTopicNum = followTopicMapper.getFollowTopicNum(releaseTopicVO.getReleaseTopicTopicId());
            //创建Map
            Map<String, Object> map = new HashMap<>();
            //存入到map当中
            map.put("type",2);//日记为1 话题为2
            map.put("id",releaseTopicVO.getReleaseTopicTopicId());//内容id
            map.put("contentFilePath",releaseTopicVO.getTopicFilePath());//内容图片
            map.put("content",releaseTopicVO.getTopicName());//标题
            map.put("followTopicNum",followTopicNum);//被关注数量
            //判断该话题是否被 用户关注
            for (FollowTopicVO followTopicVO : followTopicList) {
                if(followTopicVO.getFollowTopicCoverTopicId()==releaseTopicVO.getReleaseTopicTopicId() && followTopicVO.getDelFlag()==0 ){
                    map.put("isFollow",true);
                    break;
                }else{
                    map.put("isFollow",false);
                }
            }
            //添加到list当中
            list.add(map);
        }

        return list;
    }

    /**
     * 查询话题推荐/最热/最新
     * @return
     */
    @Override
    public List<Map> getTopicIndexList(Integer id,String sort) {
        //定义一个List用于存放内容
        List<Map> list=new ArrayList<>();
        //查询到列表话题
        List<ReleaseTopicVO> topicByUserId = topicMapper.getTopicLabel(sort);
        //根据releaseTopic查询到点赞数 评论数
        for (ReleaseTopicVO releaseTopic : topicByUserId) {
            //根据话题找到发布话题的用户信息
            UserVO userVO=topicMapper.getUserByTopicId(releaseTopic.getReleaseTopicTopicId());
            //查询到点赞数
            int kudosNum = kudosMapper.getKudosNum(releaseTopic.getReleaseTopicTopicId(), 2);
            //查询该用户是否点赞过
            KudosEntity kudosById = kudosMapper.getKudosById(releaseTopic.getReleaseTopicTopicId(), 2, id);
            //定义变量 判断是否点赞
            Boolean isKudosByUserId;
            //判断KudosEntity是否为空，kudos_whether是否为0
            if(kudosById==null || kudosById.getKudosWhether()==1){
                //没点赞
                isKudosByUserId=false;
            }else{
                //点赞了
                isKudosByUserId=true;
            }
            //创建Map
            Map<String, Object> map = new HashMap<>();
            //存入到map当中
            map.put("type",2);//日记为1 话题为2
            map.put("userName",userVO.getUserName());//用户名称
            map.put("userFilePath",userVO.getUserFilePath());//用户头像
            map.put("id",releaseTopic.getReleaseTopicTopicId());//内容id
            map.put("contentFilePath",releaseTopic.getTopicFilePath());//内容图片
            map.put("content",releaseTopic.getTopicName());//内容
            map.put("kudosNum",kudosNum);//点赞数
            map.put("isKudosByUserId",isKudosByUserId);//是否点赞
            //将map添加到list当中
            list.add(map);
        }
        return list;
    }

    /**
     * 查询id话题列表
     * @return
     */
    @Override
    public List<TopicLabelEntiry> getTopicLabelById(Integer id) {
        List<TopicLabelEntiry> topicLabelEntiryList= topicMapper.getTopicLabelById(id);
        for (int i = 0;i < topicLabelEntiryList.size();i++) {
            String[] split = topicLabelEntiryList.get(i).getTopicFileId().split(",");
            List<String> sss = new ArrayList<>();
            for (String s : split) {
                String ss = topicMapper.selectUrls(s);
                sss.add(ss);
            }
            // 把图片存进集合里
            topicLabelEntiryList.get(i).setUrl(sss);
//            dailyRecordEntityList.get(i).setUrl(diaryMapper.selectUrl(dailyRecordEntityList.get(i).getDiaryRecordFileId()));
        }
        return topicLabelEntiryList;
    }

    /**
     * 根据id话题列表详情
     * @return
     */
    @Override
    public List<TopicEntiry> getTopicLabelByIdOne(Integer id) {
        List<TopicEntiry> topicLabelEntiryList=  topicMapper.getTopicLabelByIdOne(id);
        // 查找日记图片
        for (int i = 0;i < topicLabelEntiryList.size();i++) {
            String[] split = topicLabelEntiryList.get(i).getReleaseTopicFileId().split(",");
            List<String> sss = new ArrayList<>();
            for (String s : split) {
                String ss = topicMapper.selectUrls(s);
                sss.add(ss);
            }
            // 把图片存进集合里
            topicLabelEntiryList.get(i).setUrl(sss);
//            dailyRecordEntityList.get(i).setUrl(diaryMapper.selectUrl(dailyRecordEntityList.get(i).getDiaryRecordFileId()));
        }
        return topicLabelEntiryList;
    }

    /**
     * 查询id话题列表总数
     * @return
     */
    @Override
    public Integer getTopicLabelByIdNum(Integer id) {
        return topicMapper.getTopicLabelByIdNum(id);
    }

    /**
     * 话题增加浏览数
     * @param id
     * @return
     */
    @Override
    public Integer updetTopicLabel(Integer id) {
        return topicMapper.updetTopicLabel(id);
    }

    /**
     * 搜索话题
     * @param title
     * @return
     */
    @Override
    public List<TopicEntiry> getTopicHistoryRecord(String title) {
        return topicMapper.getTopicHistoryRecord(title);
    }

    /**
     * 新增历史记录
     * @return
     */
    @Override
    public Integer saveHistoryRecord(HistoryRecordEntiry historyRecordEntiry) {
        // 查询历史是否重复
        List<HistoryRecordEntiry> historyRecordEntiryList = topicMapper.getHistoryRecord(historyRecordEntiry.getHistoryRecordName(),historyRecordEntiry.getHistoryUserId());
        if(historyRecordEntiryList.size()>0 && historyRecordEntiryList!=null){
            return 1;
        }else {
            historyRecordEntiry.setCreateTime(new Date());
            return topicMapper.saveHistoryRecord(historyRecordEntiry);
        }
    }

    /**
     * 查询历史记录
     * @return
     */
    @Override
    public List<HistoryRecordEntiry> getHistoryRecordList(Integer id) {
        return topicMapper.getHistoryRecordList(id);
    }

    /**
     * 查询热门历史记录
     * @return
     */
    @Override
    public List<HistoryRecordEntiry> getHistoryRecordListNum() {
        return topicMapper.getHistoryRecordListNum();
    }

    /**
     * 搜索话题
     * @param title
     * @return
     */
    @Override
    public List<TopicLabelEntiry> getHistoryRecordTopic(String title) {
        List<TopicLabelEntiry> topicLabelEntiryList= topicMapper.getHistoryRecordTopic(title);
        // 查找日记图片
        for (int i = 0;i < topicLabelEntiryList.size();i++) {
            String[] split = topicLabelEntiryList.get(i).getTopicFileId().split(",");
            List<String> sss = new ArrayList<>();
            for (String s : split) {
                String ss = topicMapper.selectUrls(s);
                sss.add(ss);
            }
            // 把图片存进集合里
            topicLabelEntiryList.get(i).setUrl(sss);
//            dailyRecordEntityList.get(i).setUrl(diaryMapper.selectUrl(dailyRecordEntityList.get(i).getDiaryRecordFileId()));
        }
        return topicLabelEntiryList;
    }

    /**
     * 搜索日记
     * @return
     */
    @Override
    public List<DiaryEntity> getHistoryRecordDiary(String title) {
        return topicMapper.getHistoryRecordDiary(title);
    }

    /**
     * 搜索咨询
     * @return
     */
    @Override
    public List<InformationEntiry> getHistoryRecordInformation(String title) {
        return topicMapper.getHistoryRecordInformation(title);
    }

    /**
     * 搜索用户
     * @return
     */
    @Override
    public List<User> getHistoryRecordUser(String title) {
        return topicMapper.getHistoryRecordUser(title);
    }
}